package com.slaffka.dao;

import com.slaffka.model.Filial;

import javax.faces.bean.ApplicationScoped;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: slaffka
 * Date: 26.11.12
 * Time: 22:06
 * To change this template use File | Settings | File Templates.
 */
@Named("filialDAO")
@ApplicationScoped
public class FilialDAO implements Serializable {

    @PersistenceContext
    EntityManager entityManager;

    private List<Filial> filialList;

    public List<Filial> getFilialList(){
        return entityManager
                .createQuery("select f from Filial f order by f.filialId desc ")
                .getResultList();
    }

    public Double recalculateMultiFilialAmount(){
        Double sumAmount = null;
        try {
            sumAmount = (Double)entityManager
                    .createQuery("select sum(f.capital) from Filial f where f.abbreviation != 'multiFilial'")
                    .getSingleResult();
            Filial multiFilial = getMultiFilial();
            multiFilial.setCapital(sumAmount);
            entityManager.merge(multiFilial);
            entityManager.flush();
            return sumAmount;
        } catch (NoResultException e){
            e.printStackTrace();
            return null;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    private Filial getMultiFilial() {
        return (Filial)entityManager
                .createQuery("select f from Filial f where f.abbreviation = 'multiFilial'")
                .getSingleResult();
    }

    public void updateCapitalFilial(Long filialId, Double newCapital){
        try {
            Filial filial = entityManager.find(Filial.class, filialId);
            filial.setCapital(newCapital);
            entityManager.merge(filial);
            entityManager.flush();
            recalculateMultiFilialAmount();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void updateFilial(Filial filial) {
        try {
            Filial storedFilial = entityManager.find(Filial.class, filial.getFilialId());

            if (!filial.getCapital().equals(storedFilial.getCapital())){
                Double diff = filial.getCapital() - storedFilial.getCapital();
                storedFilial.setCapital(filial.getCapital());

                Filial multiFilial = getMultiFilial();
                Double storedCapital = multiFilial.getCapital();
                multiFilial.setCapital(storedCapital + diff);
            }
            storedFilial.setAbbreviation(filial.getAbbreviation());
            storedFilial.setName(filial.getName());
            entityManager.merge(storedFilial);
            entityManager.flush();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
